﻿using System.Linq;
using System.Web.Mvc;
using WD.Db.Contract;
using WD.Web.MVC.Common;

namespace WD.Web.MVC.Controllers.Filters
{
    /// <summary>
    /// 验证用户是否登录 
    /// </summary>
    public class AdminOnlineFilter : AuthorizeAttribute, IExceptionFilter
    {
        /// <summary>
        /// 权限URL （默认为当前请求链接）
        /// </summary>
        public string Url { get; set; }
        /// <summary>
        /// 是否捕捉异常并返回JSON （默认TRUE 捕捉异常）
        /// </summary>
        public bool IsCatchException { get; set; }
        public AdminOnlineFilter()
        {
            IsCatchException = true;
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            // 本机免验证
            //  bool isOnline = filterContext.HttpContext.Request.IsLocal || base.AuthorizeCore(filterContext.HttpContext);

            if (AdminUserContext.Current.LoginInfo == null)
            {
                filterContext.Result = new JsonResult
                    {
                        Data = new
                            {
                                statusCode = CommonCode.Json结果代码_statusCode_登录超时,
                                message = "登录超时，请重新登录！",
                                callbackType = CommonCode.Json结果代码_callbackType_关闭当前对话框
                            },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
            }
            else
            {
               
            }
        }


        //异常拦截
        public void OnException(ExceptionContext filterContext)
        {
            if (IsCatchException != false)
            {
                var requestUrl = filterContext.HttpContext.Request.Url;
                if (requestUrl != null)
                {
                    var url = Url ?? requestUrl.AbsolutePath;

                    var errMsg = url + " 出现异常，请稍后再试！<br/>异常信息：" + filterContext.Exception.Message;
                    filterContext.Result = new JsonResult
                        {
                            Data = new
                                {
                                    statusCode = StatusCode.错误,
                                    message = errMsg,
                                    callbackType = CommonCode.Json结果代码_callbackType_关闭当前对话框
                                },
                            JsonRequestBehavior = JsonRequestBehavior.AllowGet
                        };

                    // commonHandle.InsertLog(url + " - 操作失败！", CommonCode.操作日志类型_操作失败, filterContext.HttpContext,filterContext.Exception.Message);

                    //            if (filterContext.HttpContext.Session != null)
                    //            {
                    //                var u = (Rbac_User)filterContext.HttpContext.Session["User"];
                    //                filterContext.ExceptionHandled = u.UserID != "admin";        //管理员抛出异常到页面
                    //            }

                    filterContext.ExceptionHandled = true;
                }
            }
        }
    }
}